using System;
using DG.Tweening;
using UnityEngine;

namespace Dream
{
    public class ShaderController
    {
        public readonly string EYEX = "_EllipseRadiusX";
        public readonly string EYEY = "_EllipseRadiusY";

        public float eyeXOriginVal = 1000f;
        public float eyeYOriginVal = 500f;

        public ShaderController(Action action)
        {
            Shader.SetGlobalFloat(EYEX, eyeXOriginVal);
            Shader.SetGlobalFloat(EYEY, eyeYOriginVal);

            //设定回调
            this.action = action;
        }

        private event Action action;

        //mao 24-11-29 07:36
        /// <summary>
        ///     播放瞌睡动画后进入在构造函数中加入的回调
        /// </summary>
        public void Sleeping()
        {
            // 创建一个序列来执行动画
            var seq = DOTween.Sequence();

            // 添加动画步骤，每个步骤1秒
            seq.Append(DOTween.To(() => eyeYOriginVal, y => eyeYOriginVal = y, 200f, 1f)
                .OnUpdate(() => Shader.SetGlobalFloat(EYEY, eyeYOriginVal)));
            seq.Append(DOTween.To(() => eyeYOriginVal, y => eyeYOriginVal = y, 300f, 1f)
                .OnUpdate(() => Shader.SetGlobalFloat(EYEY, eyeYOriginVal)));
            seq.Append(DOTween.To(() => eyeYOriginVal, y => eyeYOriginVal = y, 100f, 1f)
                .OnUpdate(() => Shader.SetGlobalFloat(EYEY, eyeYOriginVal)));
            seq.Append(DOTween.To(() => eyeYOriginVal, y => eyeYOriginVal = y, 150f, 1f)
                .OnUpdate(() => Shader.SetGlobalFloat(EYEY, eyeYOriginVal)));
            seq.Append(DOTween.To(() => eyeYOriginVal, y => eyeYOriginVal = y, 0f, 1f)
                .OnUpdate(() => Shader.SetGlobalFloat(EYEY, eyeYOriginVal)));

            seq.onComplete += () => { action?.Invoke(); };

            // 开始执行序列
            seq.Play();
        }
    }
}